package com.config;

//import com.utils.Commons;
import com.entity.BUser;
import com.entity.FCart;
import com.entity.FUser;
import com.entity.table.FCartTableDef;
import com.service.FCartService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;

//自定义一个拦截器类
@Configuration
public class MyInterceptor implements HandlerInterceptor {
    private static final Set<String> exUrl = new HashSet<>();//不拦截的URI
    @Autowired
    private FCartService fCartService;
    static {
        exUrl.add("/login");
        exUrl.add("/toLogin");
        exUrl.add("/logout");
        exUrl.add("/uploadFile");
        exUrl.add("/download");
        exUrl.add("/toUpload");
        exUrl.add("/toDownload");
        exUrl.add("/get/");
        exUrl.add("/update/");
        exUrl.add("/api/");
        exUrl.add("/delete/");
        exUrl.add("/admin/login");
        exUrl.add("/admin/toLogin");
        exUrl.add("/register");
        exUrl.add("/toRegister");
        exUrl.add("/ueUploadController/");
        exUrl.add("/");


    }
//    @Autowired
//    private Commons commons;
    //访问控制器controller之前执行的方法
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        if (exUrl.contains(uri)) {
            return true;
        }
        // 如果session不为空，则说明用户已登录，如果为空，说明未登录，跳转到登录页面
        HttpSession session = request.getSession();
        if (uri.contains("/admin")){
            Object admin = session.getAttribute("admin");
            if (admin == null) {
                // 用户未登录，重定向到登录页面
                response.sendRedirect("/admin/login");
                return false;
            }
            request.setAttribute("admin",admin);
        }
        else {
            Object user = session.getAttribute("user");
            if (user == null) {
                // 用户未登录，重定向到登录页面
                response.sendRedirect("/login");
                return false;
            }
            request.setAttribute("user",user);
        }
        return true;
    }
    // controller 方法调用完毕后，执行此方法
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response,
                           Object handler, @Nullable ModelAndView modelAndView) throws Exception {
        HttpSession session = request.getSession();
        BUser admin = (BUser)session.getAttribute("admin");
        FUser user = (FUser)session.getAttribute("user");
        FCart fCart = new FCart();
        if (user!=null){
            fCart=fCartService.queryChain().where(FCartTableDef.F_CART.USER_ID.eq(user.getId())).one();
        }
        request.setAttribute("admin",admin);
        request.setAttribute("user",user);
        request.setAttribute("fCart",fCart);
//        request.setAttribute("currentYear", Calendar.getInstance().get(Calendar.YEAR));
//        request.setAttribute("commons",commons);
    }
    // 页面渲染完成后调用此方法, 一般用来清除某些资源等
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse
            response, Object handler, @Nullable Exception ex) throws Exception {
        System.out.println("afterCompletion");
    }
}

